package com.whw.fy.controller;

import com.whw.fy.entity.Comment;
import com.whw.fy.entity.Message;
import com.whw.fy.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;


@Api(tags = "通用评论模块")
@RestController
@RequestMapping("/api/comment")
public class CommentController {

    @Resource
    CommentService commentService;

    @ApiOperation(value = "添加评论")
    @PostMapping("/insert")
    public Message insert(Comment comment)throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (comment.getCommUpdatetimeToString()!=null)
            comment.setCommCreattime(simpleDateFormat.parse(comment.getCommCreattimeToString()));
        if (comment.getCommUpdatetimeToString()!=null)
            comment.setCommUpdatetime(simpleDateFormat.parse(comment.getCommUpdatetimeToString()));
        System.out.println(comment.getCommCreattime());
        return commentService.insertSelective(comment);
    }

    @ApiOperation(value = "删除评论记录")
    @DeleteMapping("/")
    public Message delete(int commentId) {
        return commentService.deleteByPrimaryKey(commentId);
    }

    @ApiOperation(value = "修改评论记录")
    @PutMapping("/")
    public Message update(Comment comment) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (comment.getCommUpdatetimeToString()!=null)
            comment.setCommUpdatetime(simpleDateFormat.parse(comment.getCommUpdatetimeToString()));
        return commentService.updateByPrimaryKeySelective(comment);
    }

    @ApiOperation(value = "根据评论id查找评论")
    @GetMapping("/select")
    public Message selectByPrimaryKey(Integer commentId,Integer userId){
        return commentService.selectByPrimaryKey(commentId,userId);
    }

    @ApiOperation(value = "根据评论id查找评论(管理员使用)")
    @GetMapping("/admin/select")
    public Message selectByPrimaryKeyForAdmin(Integer commentId){
        return commentService.selectByPrimaryKeyForAdmin(commentId);
    }


    @ApiOperation(value = "获得所有评论记录")
    @GetMapping("/all")
    public Message selectAll(Integer userId){
        return commentService.selectAll(userId);
    }

    @ApiOperation(value = "获得所有评论记录(管理员使用)")
    @GetMapping("/admin/all")
    public Message selectAllForAdmin(){
        return commentService.selectAllForAdmin();
    }

    @ApiOperation(value = "查询")
    @GetMapping("/selectallByall")
    public Message selectAllByAll(Comment comment){
        return commentService.selectAllByAll(comment);
    }
}
